﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Text;
using SilverlightGolf;
using SilverlightGolf.Core;
using SilverlightGolf.Physics;

namespace SilverlightGolf.Client
{
    public partial class Page : UserControl
    {
        Dictionary<int, int> framerate = new Dictionary<int, int>();
        public static GameLoop gameLoop;
        KeyHandler keyHandler;

        Game.GolfGame game;
        HolePres hole;

        Game.Player rich = new Game.Player("Rich");
        Game.Player veg = new Game.Player("Veg");

        public Page()
        {
            InitializeComponent();

            keyHandler = new KeyHandler(this);

            gameLoop = new GameLoop();
            gameLoop.Attach(parentCanvas);
            gameLoop.Update += gameLoop_Update;

            Fps fps = new Fps();

            parentCanvas.Children.Add(fps);

            game = new Game.GolfGame();

            game.Players.Add(rich);
            game.Players.Add(veg);

            game.StartNewHole();

            hole = new HolePres(game.CurrentHole);

            parentCanvas.Children.Add(hole);




            foreach (var b in hole.PlayersBalls)
            {
                b.PlayersBall.Geom.Body.ResistiveForces.Add(Friction);
                b.PlayersBall.Geom.Body.DynamicForces.Add(Bowl);
            }

            hole.PlayersBalls[0].Color = Color.FromArgb(255, 255, 0, 0);
            hole.PlayersBalls[0].Radius = 20;

            foreach (var b in game.CurrentHole.GameObjects.Filter<Game.PlayersBall>())
            {
                b.Geom.Body.Position = new Vector2(100, 100);
                b.Geom.Body.Velocity = new Vector2(300, 0);
            }
            hole.PlayersBalls[0].Ball.Geom.Body.Velocity = new Vector2(200, 100);
        }

        double Friction(Physics.Body body, double dt)
        {
            double frictionCoeff = 100.00;
            return frictionCoeff * body.Mass;
        }

        Vector2 Bowl(Physics.Body body, double dt)
        {
            double maxForce = 1000;
            double radius = 200;
            var diff = body.Position - new Vector2(200, 200);

            if (diff.Length < radius)
                return -diff.Normal * maxForce * Math.Sin(Math.PI * diff.Length / radius);
            else
                return Vector2.Zero;
        }

        void gameLoop_Update(TimeSpan elapsedTime)
        {
            var keyForce = 20.0;

            var ball1 = game.CurrentHole.GameObjects.Filter<Game.PlayersBall>()[0];

            if (keyHandler.IsKeyPressed(Key.Up))
                ball1.Geom.Body.ApplyImpulse(Vector2.UnitY * -keyForce);

            if (keyHandler.IsKeyPressed(Key.Down))
                ball1.Geom.Body.ApplyImpulse(Vector2.UnitY * keyForce);

            if (keyHandler.IsKeyPressed(Key.Left))
                ball1.Geom.Body.ApplyImpulse(Vector2.UnitX * -keyForce);

            if (keyHandler.IsKeyPressed(Key.Right))
                ball1.Geom.Body.ApplyImpulse(Vector2.UnitX * keyForce);

            //if (keyHandler.IsKeyPressed(Key.C))
            //{
            //    Random rand = new Random();
            //    ball1.Color = Color.FromArgb(255, (byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255));
            //}

            game.CurrentHole.Update();
            hole.Draw();
        }
    }
}
